package com.safewind.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.safewind.model.Address;
import com.safewind.model.AddressGroupData;
import com.safewind.model.AddressGroup;
import com.safewind.model.Send;
import com.safewind.model.User;
import com.safewind.service.IAddressGroupService;
import com.safewind.service.IAddressService;
import com.safewind.service.ISendService;

@Controller
@RequestMapping("/data")
public class DataController {

	@Resource
	private IAddressService addressService;

	@Resource
	private IAddressGroupService addressGroupService;

	@Resource
	private ISendService sendService;

	@RequestMapping("/getAddress")
	@ResponseBody
	public Object getAddress(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		List<AddressGroup> groupList = addressGroupService.getAllGroup(user
				.getPkUserId());
		List<AddressGroupData> addressDataList = new ArrayList<AddressGroupData>();
		for (AddressGroup ag : groupList) {
			AddressGroupData ad = new AddressGroupData(ag);
			List<Address> addressList = addressService.getAllByGroupId(ag
					.getPkId());
			if (addressList != null && addressList.size() != 0) {
				ad.setAddressList(addressList);
			}
			addressDataList.add(ad);
		}
		map.put("addressDataList", addressDataList);
		map.put("success", true);
		return map;
	}

	@RequestMapping("/getQuartz")
	@ResponseBody
	public Object getQuartz(HttpServletRequest request, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		List<Send> sendList = sendService.getAllQuartzByUserId(user
				.getPkUserId());
		map.put("dataList", sendList);
		map.put("success", true);
		return map;
	}
}
